<template>
  <view class="page-register">
    <template v-if="step === 1">
      <image class="ui-img" src="http://www.xinchengai.top:443/register.png" mode="aspectFit" />
      <view class="ui-text">选择你的身份</view>
      <van-field @click-input="() => showType('type')" readonly class="ui-select" border :value="form.typeText" right-icon="arrow-down"></van-field>
    </template>
    <template v-else>
      <image class="ui-img1" src="http://www.xinchengai.top:443/register1.png" mode="aspectFit" />
      <view class="ui-row">
        <view class="ui-title">姓名</view>
        <van-field class="ui-select" border :value="form.name" @change="(e) => (form.name = e.detail)"></van-field>
      </view>
      <view class="ui-row">
        <view class="ui-title">性别</view>
        <van-field @click-input="() => showType('gender')" readonly class="ui-select" border :value="form.genderText" right-icon="arrow-down"></van-field>
      </view>
      <view class="ui-row">
        <view class="ui-title">年龄</view>
        <van-field @click-input="() => showType('age')" readonly class="ui-select" border :value="form.ageText" right-icon="arrow-down"></van-field>
      </view>
      <view class="ui-row">
        <view class="ui-title">学校</view>
        <van-field class="ui-select" border :value="form.school" @change="(e) => (form.school = e.detail)"></van-field>
      </view>
      <view class="ui-row">
        <view class="ui-title">班级</view>
        <van-field @click-input="() => showType('class')" readonly class="ui-select" border :value="form.classText" right-icon="arrow-down"></van-field>
      </view>
      <view class="ui-row">
        <view class="ui-title">年级</view>
        <van-field @click-input="() => showType('grade')" readonly class="ui-select" border :value="form.gradeText" right-icon="arrow-down"></van-field>
      </view>
    </template>
    <button class="ui-btn btn-prev" v-if="step === 2" @click="step -= 1">上一步</button>
    <button class="ui-btn" @click="next">下一步</button>
    <van-popup :show="show" position="bottom" custom-style="height: 40%;" @close="show = false">
      <van-picker :columns="columns" show-toolbar @cancel="show = false" @confirm="pickerConfirm" />
    </van-popup>
  </view>
</template>

<script setup>
import { computed, reactive, ref } from "vue";
import { userInfoUpdate } from "@/api";

const show = ref(false);
const step = ref(1);
const type = ref("type");

const form = reactive({
  type: "",
  typeText: "",
  name: "",
  gender: "",
  genderText: "",
  age: "",
  ageText: "",
  school: "",
  class: "",
  classText: "",
  grade: "",
  gradeText: "",
});

const columns = computed(() => {
  if (type.value === "type") {
    return [
      {
        text: "学生",
        value: "student",
      },
      {
        text: "老师",
        value: "teacher",
      },
      {
        text: "家长",
        value: "parents",
      },
    ];
  }
  if (type.value === "gender") {
    return [
      {
        text: "男",
        value: "M",
      },
      {
        text: "女",
        value: "F",
      },
    ];
  }

  if (type.value === "age") {
    const start = form.type === "student" ? 5 : 18;
    const end = form.type === "student" ? 14 : 63;
    return Array.from({ length: end }, (x, i) => i + start).map((item) => {
      return {
        text: `${item}岁`,
        value: item,
      };
    });
  }

  if (type.value === "class") {
    return [
      {
        text: "小学1年级",
        value: "1",
      },
      {
        text: "小学2年级",
        value: "2",
      },
      {
        text: "小学3年级",
        value: "3",
      },
      {
        text: "小学3年级",
        value: "3",
      },
      {
        text: "小学4年级",
        value: "4",
      },
      {
        text: "小学5年级",
        value: "5",
      },
      {
        text: "小学6年级",
        value: "6",
      },
      {
        text: "初中1年级",
        value: "7",
      },
      {
        text: "初中2年级",
        value: "8",
      },
      {
        text: "初中3年级",
        value: "9",
      },
    ];
  }

  if (type.value === "grade") {
    return Array.from({ length: 20 }, (x, i) => i + 1).map((item) => {
      return {
        text: `${item}班`,
        value: item,
      };
    });
  }
});

const pickerConfirm = (v) => {
  const item = v.detail.value;
  form[type.value] = item.value;
  form[`${type.value}Text`] = item.text;
  show.value = false;
};

const showType = (value) => {
  type.value = value;
  show.value = true;
};

const next = () => {
  if (step.value === 2) {
    let flag = true;
    const data = JSON.parse(JSON.stringify(form));
    delete data.typeText;
    delete data.genderText;
    delete data.gradeText;
    delete data.classText;
    delete data.ageText;
    Object.keys(data).forEach((key) => {
      if (!data[key]) {
        uni.showToast({
          title: `请完善信息`,
          icon: "none",
        });
        flag = false;
        return;
      }
    });
    flag &&
      userInfoUpdate(data).then((res) => {
        uni.switchTab({
          url: "/pages/home/index",
        });
      });
  }

  if (step.value < 2) {
    step.value += 1;
  }
};
</script>

<style lang="scss">
.page-register {
  display: flex;
  align-items: center;
  min-height: 100vh;
  flex-direction: column;
  box-sizing: border-box;
  padding-bottom: 40rpx;
  .ui-img {
    margin-top: 200rpx;
    transform: scale(1.5);
    width: 100%;
    display: block;
  }
  .ui-img1 {
    margin-top: 100rpx;
    margin-bottom: 50rpx;
  }
  .ui-text {
    font-size: 38rpx;
    font-weight: bold;
    color: black;
    text-align: left;
    margin: 80rpx 0;
    width: 80%;
  }
  .ui-btn {
    background: black;
    width: 80%;
    color: #fff;
    flex: 0 0 auto;
  }
  .ui-select {
    width: 80%;
    border: 2rpx solid #000;
    border-radius: 8rpx;
    overflow: hidden;
    margin-bottom: 50rpx;
    display: block;
    font-size: 28rpx;
  }
  .ui-row {
    width: 80% !important;
    display: block;
    .ui-select {
      width: 100%;
      margin-bottom: 20rpx;
    }
    .ui-title {
      font-size: 30rpx;
      margin-bottom: 20rpx;
    }
  }
  .btn-prev {
    margin-top: 80rpx;
    margin-bottom: 40rpx;
  }
}
</style>
